package com.android.smsmessing;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.smsmessing.adapter.SMSAdapter;
import com.android.smsmessing.adapter.SendSMSAdapter;
import com.android.smsmessing.adapter.SeparatedListAdapter;
import com.android.smsmessing.model.SMSVO;
import com.android.smsmessing.services.SMSService;

public class SMSActivity extends ListActivity {
	private Context context = null;
	public SMSAdapter smsAdapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		SeparatedListAdapter adapter = new SeparatedListAdapter(this);  
		smsAdapter = new SMSAdapter(this);
		Bundle bundle = this.getIntent().getExtras();
		String number = bundle.getString("number");
		initData(this, smsAdapter, number);
		setListAdapter(smsAdapter);
		final ArrayList<SMSVO> smses = smsAdapter.getSmses();
		SMSVO firstSMSVO = smses.get(0);
		String name = firstSMSVO.name;
		if (name == null) {
			name = "";
		}
		context = this;
		setTitle(name + "<" + firstSMSVO.mobile + ">");
		
		toLastItem(getListView(), smses.size());
	}
	
	/**
	 * 移动到最后一行
	 * @param listView		listview
	 * @param maxLength		最大行数,一般是List的size
	 * @date 2010-3-17
	 */
	public void toLastItem(ListView listView,final int maxLength){
		listView.postDelayed(new Runnable() {
			@Override
			public void run() {
				getListView().setSelection(maxLength);
			}
		}, 0);
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		System.out.println("onRestart");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		System.out.println("onResume");
	}

	public void initData(Context context, SMSAdapter smsAdapter, String number) {
		ArrayList<SMSVO> smses = SMSService.getInstance().getSMSesByNumber(context, number);
		smsAdapter.setSmses(smses);
		smsAdapter.notifyDataSetChanged();
	}
}
